<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 100px;
            height: 100px;
            position: absolute;
        }
    </style>
</head>

<body>
    <div class="box1" style="background-color: pink;margin: 0px auto;">???</div>
    <div class="box2" style="background-color: yellow;margin: 400px;">!!!</div>
    <script>
        var boxx = document.querySelector(".box1")
        var boxy = document.querySelector(".box2")
        boxx.onmousedown = function (event) {
            var e = event || window.event
            var x = e.clientX - this.offsetLeft
            var y = e.clientY - this.offsetTop
            document.onmousemove = function (event) {
                var e = event || window.event
                var xx = e.clientX - x
                var yy = e.clientY - y
                boxx.style.left = xx + "px"
                boxx.style.top = yy + "px"
                // if (Math.abs(boxy.offsetLeft - this.offsetLeft) < this.offsetWidth && Math.abs(boxy.offsetTop - this.offsetTop) < this.offsetHeight) {
                //     console.log("啊！？")
                // }
                var AdaX = boxx.offsetLeft + boxx.offsetWidth
                var AxiX = boxx.offsetLeft
                var BdaX = boxy.offsetLeft + boxy.offsetWidth
                var BxiX = boxy.offsetLeft
                var AdaY = boxx.offsetTop + boxx.offsetHeight
                var AxiY = boxx.offsetTop
                var BdaY = boxy.offsetTop + boxy.offsetHeight
                var BxiY = boxy.offsetTop
                if (AdaX > BxiX && AxiX < BdaX && AdaY > BxiY && AxiY < BdaY) { console.log("啊？乖乖站好") }
            }
            boxx.onmouseup = function () {
                document.onmousemove = ""
            }
        }
    </script>
</body>

</html>